package com.example.weli.ble2test;

import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.text.SpannableStringBuilder;
import android.text.style.RelativeSizeSpan;
import android.text.style.SubscriptSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

public class Slidercontroller {
    private String danwei = BuildConfig.FLAVOR;
    private boolean invert = false;
    private UIActivity mActivity;
    private Drawable mBlue;
    private Drawable mGreen;
    private int mLEDNumber;
    private Drawable mRed;
    private boolean nomax = false;
    private int posoffset = 0;
    private Slider thisslider;

    class LabelClickListener implements OnClickListener {
        private final Slider mSlider;
        private final double mValue;

        public LabelClickListener(Slider slider, double value) {
            this.mSlider = slider;
            this.mValue = value;
        }

        public void onClick(View v) {
            this.mSlider.setPosition(this.mValue);
        }
    }

    class LedValueUpdater implements Slider.SliderPositionListener {
        private final byte mCommandTarget;
        private TextView mTarget;

        LedValueUpdater(TextView target, int colorIndex) {
            this.mTarget = target;
            this.mCommandTarget = (byte) colorIndex;
        }

        public void onPositionChange(double value) {
            int v = (int) value;
            if (Slidercontroller.this.invert) {
                v = Slidercontroller.this.posoffset - v;
            }
            if (Slidercontroller.this.nomax) {
                this.mTarget.setText(String.valueOf(v - Slidercontroller.this.posoffset) + Slidercontroller.this.danwei);
            } else {
                this.mTarget.setText(String.valueOf(v - Slidercontroller.this.posoffset) + Slidercontroller.this.danwei);
            }
            if (Slidercontroller.this.mActivity != null) {
                Slidercontroller.this.mActivity.servo_slider(this.mCommandTarget, v);
            }
        }

        public void disPosition(double value) {
            int v = (int) value;
            if (Slidercontroller.this.invert) {
                v = Slidercontroller.this.posoffset - v;
            }
            if (Slidercontroller.this.nomax) {
                this.mTarget.setText(String.valueOf(v - Slidercontroller.this.posoffset) + Slidercontroller.this.danwei);
            } else {
                this.mTarget.setText(String.valueOf(v - Slidercontroller.this.posoffset) + Slidercontroller.this.danwei);
            }
        }
    }

    public void set_bascv(String dan_wer, int ofset) {
        this.danwei = dan_wer;
        this.posoffset = ofset;
    }

    public void setnomax(boolean is) {
        this.nomax = is;
    }

    public void setinveter() {
        this.invert = true;
    }

    public Slidercontroller(UIActivity activity, int number, Resources res, boolean vertical) {
        this.mActivity = activity;
        this.mLEDNumber = number;
        if (vertical) {
            this.mRed = res.getDrawable(R.color.butc);
            this.mGreen = res.getDrawable(R.color.grey33);
            this.mBlue = res.getDrawable(R.color.white_);
            return;
        }
        this.mRed = res.getDrawable(R.color.butc);
        this.mGreen = res.getDrawable(R.color.grey33);
        this.mBlue = res.getDrawable(R.color.white_);
    }

    public void attachToView(ViewGroup targetView, int number) {
        int v = 0;
        ViewGroup g = targetView;
        TextView label = (TextView) g.getChildAt(0);
        Slider slider = (Slider) g.getChildAt(1);
        this.thisslider = slider;
        TextView valueText = (TextView) g.getChildAt(2);
        slider.setPositionListener(new LedValueUpdater(valueText, number));
        if (this.invert) {
            v = this.posoffset - 0;
        }
        if (this.nomax) {
            valueText.setText(String.valueOf(v - this.posoffset) + this.danwei);
        } else {
            valueText.setText(String.valueOf(v - this.posoffset) + " /" + this.posoffset + this.danwei);
        }
        String labelText = "Led";
        SpannableStringBuilder ssb = new SpannableStringBuilder(labelText);
        ssb.append(String.valueOf(this.mLEDNumber));
        int spanStart = labelText.length();
        int spanEnd = spanStart + 1;
        ssb.setSpan(new SubscriptSpan(), spanStart, spanEnd, 0);
        ssb.setSpan(new RelativeSizeSpan(0.7f), spanStart, spanEnd, 0);
    }
}
